package com.pay.cloud.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;

import com.pay.cloud.BindState;
import com.pay.cloud.IconFontFactory;
import com.pay.cloud.R;
import com.pay.cloud.SubScreenFactory;
import com.pay.cloud.activity.AbstractActivity;

import static com.pay.cloud.fragment.TipFragment.TipType.WAIT;

public class RefundIngAbstract extends TipVerticalFragment implements BindState {

    public RefundIngAbstract() {
        super(WAIT, "正在申请退款");
    }

    @Override
    public void onPause() {
        super.onPause();
        SubScreenFactory.startSubScreenToWelcome(getContext());
    }

    @Override
    public void onError(String msg) {
        showToast(msg);
        showRefundFail();
    }

    @Override
    public boolean useNetwork() {
        return true;
    }

    protected void showRefundSubmit() {
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefundSubmit);
        onChange(TipType.SUCCESS, R.string.refund_submit);
        speak(getString(R.string.refund_submit));
    }

    protected void showRefunding() {
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefunding);
        onChange(TipType.WAIT, R.string.refund_refunding);
    }

    protected void showRefundSuccess() {
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefundSuccess);
        onChange(TipType.SUCCESS, R.string.refund_success);
        speak(getString(R.string.refund_success));
    }

    protected void showRefundFail() {
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefundFail);
        onChange(TipType.FAIL, R.string.refund_fail);
        speak(getString(R.string.refund_fail));
    }

    protected void showRefundExpired() {
        SubScreenFactory.startSubScreen(this::flushSubScreenToRefundExpired);
        onChange(TipType.EXPIRED, R.string.refund_expired);
        speak(getString(R.string.refund_expired));
    }

    private void flushSubScreenToRefundSubmit() {
        flushSubScreenToRefundStatus(R.color.colorSuccess, R.string.glyphicon_ok_sign, R.string.refund_submit);
    }

    private void flushSubScreenToRefunding() {
        flushSubScreenToRefundStatus(R.color.colorWarning, R.string.glyphicon_exclamation_sign, R.string.refund_refunding);
    }

    private void flushSubScreenToRefundSuccess() {
        flushSubScreenToRefundStatus(R.color.colorSuccess, R.string.glyphicon_ok_sign, R.string.refund_success);
    }

    private void flushSubScreenToRefundFail() {
        flushSubScreenToRefundStatus(R.color.colorDanger, R.string.glyphicon_remove_sign, R.string.refund_fail);
    }

    private void flushSubScreenToRefundExpired() {
        flushSubScreenToRefundStatus(R.color.colorDanger, R.string.glyphicon_remove_sign, R.string.refund_expired);
    }

    private void flushSubScreenToRefundStatus(int colorId, int iconId, int textId) {
        Context context = getContext();
        if (null == context) {
            return;
        }

        Bitmap bitmap = SubScreenFactory.createBitmap(context);
        Canvas canvas = new Canvas(bitmap);
        //canvas.drawColor(Color.WHITE);

        Paint iconPaint = new Paint();
        iconPaint.setAntiAlias(true);
        iconPaint.setTextSize(70f);
        iconPaint.setColor(context.getColor(colorId));
        Typeface typeface = IconFontFactory.getHalflings(context);
        iconPaint.setTypeface(typeface);

        String icon = getString(iconId);
        int iconWidth = (int) iconPaint.measureText(icon);
        int startX = (bitmap.getWidth() - iconWidth) / 2;
        canvas.drawText(icon, startX, 150, iconPaint);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(30f);

        String text = getString(textId);
        int fontWidth = (int) paint.measureText(text);
        startX = (bitmap.getWidth() - fontWidth) / 2;
        canvas.drawText(text, startX, 220, paint);

        canvas.save();
        canvas.restore();

        SubScreenFactory.flush(bitmap);

    }

}
